package vashion.azeroth.core.service.impl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.pojo.UserExperience;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalConnectionService;

@Service
public class PersonalConnectionServiceImpl<T> implements PersonalConnectionService<T> {


//	@Autowired
//	private PersonalProfilesManager personalProfilesManager;

	@Autowired
	private PersonalMemberManager personalMemberManager;

	
	public double getOperateMyExperience(UserExperience higher,
			UserExperience[] lowers, int myBaseExp) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("unchecked")
	public ApiResult<T> getHigher(PersonalMember member) {
		ApiResult<T> result = new ApiResult<T>();
		PersonalMember resMember = personalMemberManager.get(member.getMemberId());
//		PersonalMember resMember = personalMemberManager.findHigherBymemberId(member);
		try {
			if (null == resMember || null == resMember.getInviteMemberId()) {
				return ApiResult.newErrorResult(MemberErrorEnum.HIGHER_NULL.getErrorCode(), MemberErrorEnum.HIGHER_NULL.getErrorMessage());
			}
			
			PersonalMember highMember = personalMemberManager.get(resMember.getInviteMemberId());
			result = (ApiResult<T>) ApiResult.newSuccessResult(highMember);
		} catch (Exception e) {
			
		}
		return result;
	}

	public ApiResult<ArrayList<PersonalMember>> getLowers(PersonalMember member) {
		ApiResult<ArrayList<PersonalMember>> result = new ApiResult<ArrayList<PersonalMember>>();
		try {
			ArrayList<PersonalMember> resMemberList = (ArrayList<PersonalMember>) personalMemberManager.findLowererByInviteMemberId(member);
			if (null == resMemberList || resMemberList.size() == 0) {
				return ApiResult.newErrorResult(MemberErrorEnum.LOWER_NULL.getErrorCode(), MemberErrorEnum.LOWER_NULL.getErrorMessage());
			}
			ArrayList<PersonalMember> lowersList = new ArrayList<PersonalMember>();
			for (int i = 0; i < resMemberList.size(); i++) {
				
				PersonalMember lowerMember = personalMemberManager.get(resMemberList.get(i).getMemberId());
				lowersList.add(lowerMember);
			}
			result = ApiResult.newSuccessResult(lowersList);
		} catch (Exception e) {
			System.out.println(e);
		}
		return result;
	}
	
	
	
	
	
	public ArrayList<PersonalMember> getLowerList(PersonalMember member) {
		ArrayList<PersonalMember> lowersList = new ArrayList<PersonalMember>();
		try {
			ArrayList<PersonalMember> resMemberList = personalMemberManager.findLowererByInviteMemberId(member);
			if (null == resMemberList || resMemberList.size() == 0) {
				return lowersList;
			}
			for (int i = 0; i < resMemberList.size(); i++) {
				
				PersonalMember lowerMember = personalMemberManager.get(resMemberList.get(i).getMemberId());
				lowersList.add(lowerMember);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return lowersList;
	}
	
	public PersonalMember getHighMember(PersonalMember member) {
		PersonalMember highMember = new PersonalMember();
		PersonalMember resMember = personalMemberManager.get(member.getMemberId());
		try {
			if (null == resMember || null == resMember.getInviteMemberId()) {
				return null;
			}
			highMember = personalMemberManager.get(resMember.getInviteMemberId());
		} catch (Exception e) {
			
		}
		return highMember;
	}
	
	
}
